package com.kitchen.api.controller;

import com.kitchen.beans.vo.FoodVO;
import com.kitchen.beans.vo.LikeVo;
import com.kitchen.common.vo.ResultVO;
import com.kitchen.service.IndexService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.io.FilenameUtils;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@CrossOrigin
@RestController
@RequestMapping(value = "/index")
@Api(value = "提供首页信息接口",tags = "首页")
public class IndexController {

    @Autowired
    IndexService indexService;

    @GetMapping("/index")
    @ApiOperation("首页菜品的信息，根据需求使用不同的数据字段")
//    @Cacheable(cacheNames = "food")
    public ResultVO index(){
        ResultVO hotfood=indexService.hotindex();
        return hotfood;
    }

    @GetMapping("/detail/{id}")
    @ApiOperation("菜品的详细信息")
    @ApiImplicitParam(name = "id" , value = "菜品id",required = true)
//    @Cacheable(cacheNames = "detail")
    public ResultVO detail(@PathVariable("id")String id){

        ResultVO resultVO=indexService.detail(id);

        return resultVO;
    }
    @PostMapping("/add")
    @ApiOperation("菜品的上传")
    public ResultVO add(FoodVO foodVO, MultipartFile[] files) throws IOException {
        List<String> filesPath = new ArrayList<>();
        for (MultipartFile file : files) {
            String file1 = file(file);
            filesPath.add(file1);
        }

        ResultVO resultVO=indexService.add(foodVO,filesPath);
        return resultVO;
    }
    public static String file(MultipartFile file) throws IOException {
        String Filename = file.getOriginalFilename();
        //定制全局唯一的命名
        String unique = UUID.randomUUID().toString();
        //获得文件的后缀
        String extension = FilenameUtils.getExtension(Filename);
        //定制全局唯一的文件名
        String uniqueFileName = unique+"."+extension;
        file.transferTo(new File("D:/kitchen/"+uniqueFileName));
        return uniqueFileName ;
    }

    @GetMapping("/img")
    @ApiOperation("获取图片")
    @Cacheable(cacheNames = "src")
    @ApiImplicitParam(name = "src" , value = "图片路径，一般为获取到的图片路径",required = true)
    public void img(String src,HttpServletResponse response) throws IOException {
            //读取目标文件，写出给客户端
            IOUtils.copy(new FileInputStream("D:/kitchen/"+src), response.getOutputStream());
    }

    @GetMapping("/delete/{id}")
    @ApiOperation("删除菜谱")
    @ApiImplicitParam(name = "id" , value = "菜谱的id",required = true)
    public ResultVO delete(@PathVariable("id")int id){
        ResultVO resultVO=indexService.delete(id);
        return resultVO;
    }
    @GetMapping("/userfoods/{id}")
    @ApiOperation("获取当前用户所发布的")
    @ApiImplicitParam(name = "id" , value = "用户的id",required = true)
    public ResultVO userfoods(@PathVariable("id")int id){
        ResultVO resultVO=indexService.selectbyuser(id);
        return resultVO;
    }
    @PostMapping("/userlike")
    @ApiOperation("大家上传的作品")
    public ResultVO userlike(@RequestBody FoodVO foodVO){
        ResultVO resultVO=indexService.selectbyname(foodVO);
        return resultVO;
    }

    @PostMapping("/like")
    @ApiOperation("菜谱点赞")
    public ResultVO userlike(@RequestBody LikeVo likeVo){
        ResultVO resultVO=indexService.like(likeVo);
        return resultVO;
    }
}
